#include "StdAfx.h"
#include "WindowedUI.h"


CWindowedUI::CWindowedUI()
: m_pParentUIElement(NULL)
{
}

CWindowedUI::~CWindowedUI()
{
}


//////////////////////////////////////////////////////////////////////////
//

void CWindowedUI::Move(int x, int y, int cx, int cy)
{
	if(m_hwndWindow != NULL && ::IsWindow(m_hwndWindow))
	{
		if(m_pParentUIElement != NULL)
		{
			//TEST m_pParentUIElement->NotifyChildResized(static_cast<IUIElement*>(this), CSize(cx, cy));
		}
		else
		{
			::MoveWindow(m_hwndWindow, x, y, cx, cy, true);
		}
	}
}

void CWindowedUI::Hide()
{
	if(m_hwndWindow != NULL && ::IsWindow(m_hwndWindow))
	{
		::ShowWindow(m_hwndWindow, SW_HIDE);
	}
}

void CWindowedUI::Show()
{
	if(m_hwndWindow != NULL && ::IsWindow(m_hwndWindow))
	{
		::ShowWindow(m_hwndWindow, SW_SHOWNORMAL);
	}
}

bool CWindowedUI::RecalculateSize(SIZE szAvailabe, SIZE* /*pszNewSize*/, int* pnWindowCount)

{
	if(pnWindowCount != NULL)
	{
		++*pnWindowCount;
	}

	return true;
}

HDWP CWindowedUI::DeferWindowPosition(
	HDWP hWinPosInfo, int x, int y, int cx, int cy, UINT uFlags)
{
	if(m_hwndWindow != NULL && ::IsWindow(m_hwndWindow))
	{
		return ::DeferWindowPos(hWinPosInfo, m_hwndWindow, NULL, x, y, cx, cy, uFlags);
	}

	return hWinPosInfo;
}


//////////////////////////////////////////////////////////////////////////
//

void CWindowedUI::Attach(HWND hwnd)
{
	m_hwndWindow = hwnd;
}

void CWindowedUI::Detach()
{
	m_hwndWindow = NULL;
}

RECT CWindowedUI::GetRectangle()
{
	_ASSERTE(m_hwndWindow != NULL);
	_ASSERTE(::IsWindow(m_hwndWindow));
	if(m_hwndWindow == NULL || !::IsWindow(m_hwndWindow))
	{
		RECT rcEmpty = { 0 };
		return rcEmpty;
	}

	RECT rcClient = { 0 };
	::GetWindowRect(m_hwndWindow, &rcClient);

	HWND hwndParent = ::GetParent(m_hwndWindow);
	if(hwndParent != NULL && ::IsWindow(hwndParent))
	{
		::ScreenToClient(hwndParent, (LPPOINT)(&rcClient));
		::ScreenToClient(hwndParent, ((LPPOINT)(&rcClient))+1);
	}
	
	return rcClient;
}


